package com.system.queue.query;

import com.system.queue.constant.CommonConstant;
import lombok.Data;
import lombok.Getter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

/**
 * @className: BaseQuery
 * @description: TODO 类描述
 * @author: Kuangkeli
 * @date: 2023/12/07 19:24
 **/
@Data
public class BaseQuery<T> {

    private Integer pageNum;

    private Integer pageSize;

    @Getter
    private String keywords;


    public Integer getPageNum() {
        if (pageNum == null) {
            return CommonConstant.PAGE_NUM;
        }
        return pageNum;
    }

    public Integer getPageSize() {
        if (pageSize == null) {
            return CommonConstant.PAGE_SIZE;
        }
        return pageSize;
    }

    public Pageable toPageable() {
        if (pageNum == null || pageSize == null){
            pageSize = CommonConstant.PAGE_SIZE;
            pageNum = CommonConstant.PAGE_NUM;
        }
        Sort sort = Sort.by(Sort.Direction.DESC, "priority"); // 按照 priority 字段升序排序
        return PageRequest.of(pageNum - 1, pageSize,sort);
    }

}
